using Cysharp.Threading.Tasks;
using DG.Tweening;
using Light;
using Light.Assets.Scripts.EventManager;
using UnityEngine;
using UnityEngine.UI;

namespace Dream
{
    public class EndPanelController : MonoBehaviour
    {
        [SerializeField] private Text text;

        [SerializeField] [TextArea] private string words;

        //播放完成话语需要的时间
        [SerializeField] private float duration;

        [SerializeField] private GameObject panel;

        private ShaderController _sleepShaderController;

        private void Awake()
        {
            EventManager.Register<EnterEndingPanelEvent>(OnEnterEndingPanelEventHandle);
            _sleepShaderController = new ShaderController(OnSleepingComplete);
        }


        private void OnDestroy()
        {
            EventManager.Unregister<EnterEndingPanelEvent>(OnEnterEndingPanelEventHandle);
        }

        private void OnEnterEndingPanelEventHandle(EnterEndingPanelEvent obj)
        {
            panel.SetActive(true);
            _sleepShaderController.Sleeping();
        }

        private async void OnSleepingComplete()
        {
            await UniTask.Delay(1000);
            text.DOText(words, duration).onComplete += () => Application.Quit();
        }
    }
}